package com.sniper.web.shiro;

import com.sniper.web.shiro.frontInter.FrontShiroInter;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

import java.util.Collection;

/**
 * 多realm管理
 * Created by suzhen on 2017/8/2.
 */
public class MyModularRealmAuthenticator extends ModularRealmAuthenticator {


    //根据类型返回不同realm
    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
        this.assertRealmsConfigured();
        Collection<Realm> realms = this.getRealms();
        Realm realm = null;

        if (authenticationToken instanceof FrontShiroInter) {
            for (Realm realm1 : realms) {
                if (realm1 instanceof FrontShiroInter) {
                    realm = realm1;
                }
            }
        } else {
            for (Realm realm1 : realms) {
                if (!(realm1 instanceof FrontShiroInter)) {
                    realm = realm1;
                }
            }
        }
        return this.doSingleRealmAuthentication(realm, authenticationToken);
    }
}
